﻿using NFine.Code;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;
using NFine.Repository.Biz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NFine.Application.Biz
{
  public  class AddressApp
    {
        private IAddressRepository service = new AddressRepository();

        public List<AddressEntity> GetList(string userid)
        {
            return service.IQueryable(a=>a.F_UserId==userid).ToList();
        }
      
        public AddressEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public int SubmitForm(AddressEntity entity)
        {
            //if (entity.F_Default == 1)
            //{
            //    service.updateDefault(entity.F_UserId);
            //}
            if (!string.IsNullOrEmpty(entity.F_Id))
            {
                return service.Update(entity);
            }
            else
            {
                var list = GetList(entity.F_UserId);
                foreach (var add in list)
                {
                    add.F_Default = 0;
                    service.Update(add);
                }
                entity.F_Id = Common.GuId();
                entity.F_Default = 1;
                return service.Insert(entity);
            }
        }
        public int DeleteForm(string keyValue)
        {
            try
            {
                return service.Delete(t => keyValue.Contains(t.F_Id));
            }
            catch
            {
                return 0;
            }
        }
    }
}

